#include <iostream>
#include <memory>
using std::cout;
using std::endl;
using std::shared_ptr;
using std::weak_ptr;

void test0(){
    weak_ptr<int> wp;
    shared_ptr<int> sp(new int(5));
    /* shared_ptr<int> sp; */


    /* shared_ptr<int> sp2(sp); */
    weak_ptr<int> wp2(sp);
    wp = sp;
    cout << wp.use_count() << endl;

    //weak_ptr不能直接访问其指向的资源
    /* *wp; */

    //没有管理资源的shared_ptr隐式转换为false
    //管理有效资源的shared_ptr隐式转换为true
    //weak_ptr无法这样使用
    if(sp){
        cout << "hello" << endl;
    }
}

weak_ptr<int> wp2;

void test1(){
    weak_ptr<int> wp;
    shared_ptr<int> sp(new int(5));
    wp = sp;
    wp2 = sp;

    shared_ptr<int> sp2 = wp.lock();
    cout << wp.use_count() << endl;

    if(sp2){
        cout << "提升成功" << endl;
        cout << *sp2 << endl;
    }else{
        cout << "提升失败，托管的空间不存在" << endl;
    }

    //wp关联的空间还在，返回false
    bool flag = wp.expired();
    if(flag){
        cout << "托管的空间已经被销毁" << endl;
    }else{
        cout << "托管的空间还在" << endl;
    }
}

int main(void){
    test1();
    cout << endl;
    bool flag = wp2.expired();
    if(flag){
        cout << "托管的空间已经被销毁" << endl;
    }else{
        cout << "托管的空间还在" << endl;
    }
    return 0;
}
